<template>
  <div>
    <div class="title">
      <i class="el-icon-sunny">账号密码找回</i>
    </div>
    <video
        style="position: absolute; z-index: -1; width: 1900px"
        class="mp41"
        src="../assets/img/111.png"
        autoplay="autoplay"
        loop="loop"
        muted="muted"
    ></video>
    <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="
        color: #FFF;
        position: absolute;
        z-index: 1;
        width: 500px;
        margin-left: 650px;
        margin-top: 200px;
      ">
      <i class="tips">请输入您注册时的账号和真实姓名</i>
      <el-form-item label="用户名" prop="username">
        <el-input v-model.number="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
            placeholder="手机号"
            prefix-icon="el-icon-phone-outline"
            v-model="mobile"
            clearable
            @blur="check_mobile">
        </el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="sms">
        <el-input
            placeholder="验证码"
            prefix-icon="el-icon-chat-line-round"
            v-model="code"
            clearable>
          <template slot="append">
            <span class="sms" @click="send_sms">{{ sms_interval }}</span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
        >提交
        </el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script scoped>
import Reset_Password from "./Reset_Password";

export default {
  components: {Reset_Password},
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      code: '',
      sms_interval: '获取验证码',
      is_send: false,
      mobile: "",
      ruleForm: {
        name: "",
        username: "",
      },
      rules: {
        pass: [{validator: validatePass, trigger: "blur"}],
        checkPass: [{validator: validatePass2, trigger: "blur"}],
        username: [{validator: checkAge, trigger: "blur"}],
      },
    };
  },
  methods: {
    check_mobile() {
      if (!this.mobile) return;
      if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
        this.$message({
          message: '手机号有误',
          type: 'warning',
          duration: 1000,
          onClose: () => {
            this.mobile = '';
          }
        });
        return false;
      }
      this.is_send = true;
      this.$axios.post(this.$settings.BASE_URL + 'user/mobile/check_mobile/', {'mobile': this.mobile}).then(response => {
        let type = ''
        if (response.data.code === 1000) {
          type = 'warning'
        } else {
          type = 'success'
        }
        this.$message({
          message: response.data.msg,
          type: type,
          duration: 1000,
        });
      })
    },
    send_sms() {
      if (!this.is_send) return;
      this.is_send = false;
      let sms_interval_time = 60;
      this.sms_interval = "发送中...";
      this.$axios.post(this.$settings.BASE_URL + 'user/code/code_cache/', {'mobile': this.mobile}).then(response => {
        console.log(response)
      })
      let timer = setInterval(() => {
        if (sms_interval_time <= 1) {
          clearInterval(timer);
          this.sms_interval = "获取验证码";
          this.is_send = true; // 重新回复点击发送功能的条件
        } else {
          sms_interval_time -= 1;
          this.sms_interval = `${sms_interval_time}秒后再发`;
        }
      }, 1000);
    },
    submitForm(formName) {
      //axios修改密码请求
      this.$axios
          .post(this.$settings.BASE_URL + 'user/userinfo/user_forget/', {
            username: this.ruleForm.username,
            name: this.ruleForm.name,
            mobile: this.mobile,
            code: this.code,
          })
          .then((Response) => {
            console.log(Response.data)
            if (Response.data.code === 100) {
              this.$setSessionStorage("user", Response.data.user);
              // alert("您的账号信息为:\n" + "用户编号：" + user.id + "\n" + "用户名：" + user.userName + "\n" + "用户密码：" + user.password + "\n" + "真实姓名：" + user.name + "\n" + "点击确定将跳转到登录页面，请妥善保存账号信息");
              this.$router.push({
                path: "/reset",
              });
            } else {
              this.$message({
                message: '信息认证失败',
                type: 'warning',
                duration: 1000
              })
            }
          })
          .catch((error) => {
            console.error(error);
          });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.title {
  position: absolute;
  z-index: 0;
  margin-left: 750px;
  margin-top: 50px;
  color: #e89797;
  font-size: 50px;
}

.tips {
  position: absolute;
  z-index: 1;
  width: 500px;
  margin-left: 270px;
  margin-top: 250px;
}

.sms {
  color: orange;
  cursor: pointer;
  display: inline-block;
  width: 70px;
  text-align: center;
  user-select: none;
}
</style>